package com.huluobo.a2111douyindemo.setting;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.huluobo.mvvm_base.mvvm.base.viewmodel.BaseViewModel;
import com.huluobo.mvvm_base.mvvm.bean.SettingBeanResult;
import com.huluobo.mvvm_base.mvvm.bean.UserBeanResult;
import com.huluobo.mvvm_base.mvvm.net.Const;

import java.io.File;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by LC on 2024/3/13.
 */
public class SettingViewModel extends BaseViewModel<SettingRepo> {
    MutableLiveData<SettingBeanResult> updateIconSuccess = new MutableLiveData<>();
    MutableLiveData<String> updateIconFailed = new MutableLiveData<>();
    MutableLiveData<UserBeanResult.UserBean> getUserInfoSuccess = new MutableLiveData<>();
    MutableLiveData<String> getUserInfoFailed = new MutableLiveData<>();
    MutableLiveData<SettingBeanResult> updateAddressSuccess = new MutableLiveData<>();
    MutableLiveData<String> updateAddressFailed = new MutableLiveData<>();
    MutableLiveData<SettingBeanResult> updateNicknameSuccess = new MutableLiveData<>();
    MutableLiveData<String> updateNicknameFailed = new MutableLiveData<>();
    MutableLiveData<SettingBeanResult> updateBrithSuccess = new MutableLiveData<>();
    MutableLiveData<String> updateBrithFailed = new MutableLiveData<>();
    MutableLiveData<SettingBeanResult> updateSexSuccess = new MutableLiveData<>();
    MutableLiveData<String> updateSexFailed = new MutableLiveData<>();

    public void updateUserNickname(String nikeName) {
        repo.updateNickname(nikeName, updateNicknameSuccess, updateNicknameFailed);
    }

    public void updateAddress(String address) {
        repo.updateAddress(address, updateAddressSuccess, updateAddressFailed);
    }

    public void updateBrith(String brith) {
        repo.updateBrith(brith, updateBrithSuccess, updateBrithFailed);
    }

    public void updateSex(String sex) {
        repo.updateSex(sex, updateSexSuccess, updateSexFailed);
    }

    public void getUserInfo(String username) {
        repo.queryUserInfoByName(username, getUserInfoSuccess, getUserInfoFailed);
    }

    public void updateUserIcon(String filePath) {
        //1.创建文件对象
        File file = new File(filePath);
        //2.封装body
        RequestBody requestBody = RequestBody.create(MediaType.parse(Const.MEDIA_PARSE), file);
        //3.封装成Part
        // file 是接口文档中文件的参数 第一个参数是接口文档中的参数值,第二个参数是文件名,可以通过file获取,第三个参数是生成好的body
        MultipartBody.Part part = MultipartBody.Part.createFormData(Const.PARAM_ICON, file.getName(), requestBody);
        repo.updateUserIcon(part, updateIconSuccess, updateIconFailed);
    }


    public SettingViewModel(@NonNull Application application) {
        super(application);
    }

    @Override
    protected SettingRepo createRepo() {
        return new SettingRepo();
    }
}
